import Vue from 'vue';
import Router from 'vue-router';
import store from '../store';

const Home = () => import('../views/Home');
const AdviserList = () => import('../views/adviser/AdviserList.vue');
const AdviserDetail = () => import('../views/adviser/AdviserDetail.vue');
const AdviserProfile = () => import('../views/adviser/AdviserProfile.vue');
const ViewList = () => import('../views/view/ViewList.vue');
const ViewDetail = () => import('../views/view/ViewDetail.vue');
const LiveList = () => import('../views/live/LiveList.vue');
const LiveDetail = () => import('../views/live/LiveDetail.vue');
const LiveHistory = () => import('../views/live/LiveHistory.vue');
const TipsList = () => import('../views/tips/TipsList.vue');
const TipsDetail = () => import('../views/tips/TipsDetail.vue');
const TipsHistory = () => import('../views/tips/TipsHistory.vue');
const PortfolioList = () => import('../views/portfolio/PortfolioList.vue');
const PortfolioDetail = () => import('../views/portfolio/PortfolioDetail.vue');
const Login = () => import('../views/Login');
const User = () => import('../views/User');
const Search = () => import('../views/Search');

Vue.use(Router);

const routes = [
  {
    path: '/',
    component: Home,
    meta: {
      checkLogin: true,
      enablePullDownRefresh: true
    }
  },
  {
    path: '/adviser/list/',
    component: AdviserList,
    meta: {
      enablePullDownRefresh: true
    }
  },
  {
    path: '/adviser/detail/:id/',
    component: AdviserDetail,
  },
  {
    path: '/adviser/profile/:id/',
    component: AdviserProfile,
  },
  {
    path: '/view/list/',
    component: ViewList,
    meta: {
      enablePullDownRefresh: true
    }
  },
  {
    path: '/view/detail/:id/',
    component: ViewDetail,
    props: (route) => ({ preview: !!route.query.preview, id: route.params.id }),
    meta: {
      checkLogin: true
    }
  },
  {
    path: '/live/list/',
    component: LiveList,
    meta: {
      enablePullDownRefresh: true
    }
  },
  {
    path: '/live/stage/:stageId/',
    component: LiveDetail,
    props: (route) => ({ stageId: route.params.stageId }),
    // async beforeEnter(to, from ,next) {
    //   try {
    //     // if(!store.getters.isLogin) {
    //     //   await store.dispatch('login');
    //     //   next();
    //     // }
    //     next();
    //   } catch (error) {
    //     console.error(error);
    //   }
    // }
  },
  {
    path: '/live/:liveId/history/',
    component: LiveHistory,
  },
  {
    path: '/tips/list/',
    component: TipsList,
  },
  {
    path: '/tips/detail/:id/',
    component: TipsDetail,
  },
  {
    path: '/tips/:tipId/history/:id/',
    component: TipsHistory
  },
  {
    path: '/portfolio/list/',
    component: PortfolioList,
    meta: {
      enablePullDownRefresh: true
    }
  },
  {
    path: '/portfolio/detail/:id/',
    component: PortfolioDetail,
    meta: {
      checkLogin: true
    }
  },
  {
    path: '/login/:type/',
    component: Login,
    props: (route) => ({ type: route.params.type || 'mobile' })
  },
  {
    path: '/search',
    component: Search,
    props: (route) => ({ ...route.query })
  },
  {
    path: '/user/',
    component: User,
    props: (route) => ({ ...route.query }),
    meta: {
      checkLogin: true
    }
  }
];

const router = new Router({
  routes,
  mode: 'history',
});

export default router;
